﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
* 项目名称 ：APP.Ad.Service
* 项目描述 ：
* 类 名 称 ：CoreService
* 类 描 述 ：
* 所在的域 ：DESKTOP-07DQ4D0
* 命名空间 ：APP.Ad.Service
* 机器名称 ：DESKTOP-07DQ4D0 
* CLR 版本 ：4.0.30319.42000
* 作    者 ：Me.笨先生 (66623978)
* 创建时间 ：2018/2/17 11:03:21
* 更新时间 ：2018/2/17 11:03:21
* 版 本 号 ：v1.0.0.0
*******************************************************************
* Copyright @ admin 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;

using APP.Ad.Entity;
using APP.Repository;
using APP.CommonLib.Cache;
using APP.CommonLib.Utils;
using APP.CommonLib.Config;
using APP.CommonLib.Entity;
using APP.CommonLib.Extension;
using APP.CommonLib.Log;
using APP.CommonLib.XService;
using APP.Repository.SqlServer;

namespace APP.Ad.Service
{
    public class CoreService
    {

        private static readonly MsSqlContext OtherContext = new MsSqlContext(ConfigManager.GetWebConn("other"));

        #region 单例实例 Instance
        private static CoreService _instance;
        private static readonly object Lockobj = new object();

        public static CoreService Instance
        {
            get
            {
                if (_instance != null) return _instance;
                lock (Lockobj)
                {
                    if (_instance == null)
                        _instance = new CoreService();
                }
                return _instance;
            }
        }
        private CoreService()
        {

        }
        #endregion

        /// <summary>
        /// 活动列表
        /// </summary>
        /// <param name="req"></param>
        /// <param name="deviceType"></param>
        /// <param name="isCache"></param>
        /// <param name="isWriteCache"></param>
        /// <returns></returns>
        public BasePageResponse<ActivityListResponse> ActivityList(ServiceRequest req, int? deviceType = 0, bool isCache = false, bool isWriteCache = false)
        {
            try
            {
                //获取参数
                var para = JsonHelper.JsonDeserialize<ActivityListParameter>(req.data);

                //是否缓存
                if (isCache)
                {
                    var activityInfoStr = CacheManager.GetRedisCache(AdCacheString.ActiveCacheSpace, "List" + para.PageIndex);
                    if (!string.IsNullOrEmpty(activityInfoStr))
                    {
                        var cacheList = JsonHelper.JsonDeserialize<BasePageResponse<ActivityListResponse>>(activityInfoStr);
                        if (cacheList != null && cacheList.Grid.Count > 0)
                            return cacheList;
                    }
                }

                //判断是否大于当前页数继续查询数据
                var listCount = OtherContext.Query<ActivityEntity>().Where(t => t.IsDelete == 0).Select(t => Sql.Count()).First();

                var allCount = Math.Ceiling(decimal.Parse(listCount.ToString()) / para.PageSize);

                var activityList = OtherContext.Query<ActivityEntity>().Where(t => t.IsDelete == 0).ToList();

                var responseEntity = new ActivityListResponse();
                var list = new List<ActivityListResponse>();
                if (activityList.Count > 0)
                {
                    foreach (var entity in activityList)
                    {
                        responseEntity.Id = entity.Id;
                        responseEntity.Title = entity.Title;
                        responseEntity.LinkUrl = entity.EndShowTime > DateTime.Now ? entity.LinkUrl : "";
                        responseEntity.ImageUrl = entity.ImageUrl;
                        responseEntity.EndImageUrl = entity.EndImageUrl;
                        responseEntity.StartShowTime = (DateTime)entity.StartShowTime;
                        responseEntity.EndShowTime = (DateTime)entity.EndShowTime;
                        list.Add(responseEntity);
                    }
                }

                var bpr = new BasePageResponse<ActivityListResponse> { Grid = list, Records = allCount.ToInt(), Page = para.PageIndex, Rows = para.PageSize };

                //存入Cache
                if (list.Count <= 0 || !isWriteCache) return bpr;

                var listStr = JsonHelper.JsonSerializer(bpr);
                if (!string.IsNullOrEmpty(listStr))
                {
                    CacheManager.PutRedisCache(AdCacheString.ActiveCacheSpace, "NewActivity_" + para.PageIndex, listStr, Common.CacheTime);
                }

                //返回对象
                return bpr;
            }
            catch (Exception ex)
            {
                Logger.Error("AdService.ActivityList", ex);
                return null;
            }
        }
    }
}
